package com.dashu.base.utils.encryption;

import javax.crypto.Cipher;
import java.nio.charset.Charset;
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;

public class RSA {


    public static void main(String[] args) {

//        String msg = encrypt("{\"areaId\":\"100224345\",\"beans\":[{\"personId\":\"8b5a15736d224ddf98e30a4a7b4e267f\",\"timestamp\":\"1575878618\",\"inOut\":\"in\"},{\"personId\":\"d6d0b63d6122468f9f9d53645a07ab03\",\"timestamp\":\"1575878572\",\"inOut\":\"in\"},{\"personId\":\"8c5caaa7bf6644628b064fb01b5e22c8\",\"timestamp\":\"1575878453\",\"inOut\":\"in\"},{\"personId\":\"36c68210c5fc460a8e181ce980f79c35\",\"timestamp\":\"1575878355\",\"inOut\":\"in\"},{\"personId\":\"6b67360280b44ba0ad24e09fddab290c\",\"timestamp\":\"1575878353\",\"inOut\":\"in\"},{\"personId\":\"fa6f1fdbf9324183ba771921520a7978\",\"timestamp\":\"1575878352\",\"inOut\":\"in\"},{\"personId\":\"7758ec84da08471a953541f74c672b42\",\"timestamp\":\"1575878345\",\"inOut\":\"in\"},{\"personId\":\"e0f01d11a9ed44ceba3a20ac1773dc20\",\"timestamp\":\"1575878235\",\"inOut\":\"in\"},{\"personId\":\"ae8937f3776b44ab800ed694836f8d72\",\"timestamp\":\"1575878079\",\"inOut\":\"in\"},{\"personId\":\"84c017df60a543e9813b17dba1a7154c\",\"timestamp\":\"1575877904\",\"inOut\":\"in\"},{\"personId\":\"1b02dcbdc9c1bc60e338610fd3dc0810\",\"timestamp\":\"1575877563\",\"inOut\":\"in\"},{\"personId\":\"224f2bd0eed947f597adf10513feee17\",\"timestamp\":\"1575877397\",\"inOut\":\"in\"},{\"personId\":\"779d1e160d544629aadb8f89b9b5267b\",\"timestamp\":\"1575877301\",\"inOut\":\"in\"},{\"personId\":\"7ead876a45c742a190ef3c2c91251726\",\"timestamp\":\"1575877180\",\"inOut\":\"in\"},{\"personId\":\"e330672170b44b529bf828cf82ff9d0a\",\"timestamp\":\"1575877062\",\"inOut\":\"in\"},{\"personId\":\"1b02dcbdc9c1bc60e338610fd3dc0810\",\"timestamp\":\"1575876846\",\"inOut\":\"in\"},{\"personId\":\"9dcadbdf556a4f00ad322d903492d0c4\",\"timestamp\":\"1575876561\",\"inOut\":\"in\"},{\"personId\":\"0ba7200c13f64a3d9dcdc2ad18be2b8a\",\"timestamp\":\"1575876113\",\"inOut\":\"in\"},{\"personId\":\"1b02dcbdc9c1bc60e338610fd3dc0810\",\"timestamp\":\"1575875950\",\"inOut\":\"in\"},{\"personId\":\"923e6f4b883b4c93b7daf2fbe03d07c2\",\"timestamp\":\"1575875911\",\"inOut\":\"in\"},{\"personId\":\"f4637217f79241e8b43969d9eca9af64\",\"timestamp\":\"1575863563\",\"inOut\":\"in\"},{\"personId\":\"5a60e92be8374fbf8a0d988313f16751\",\"timestamp\":\"1575629525\",\"inOut\":\"in\"},{\"personId\":\"1d8b081cbe5844e48d4acb236a09ba5d\",\"timestamp\":\"1575629523\",\"inOut\":\"in\"}]" , "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCYkcYHVFofkG4zGCRg6U6zDRlJskbG0cSW0i2XdKr44bCwxV7jKUVPMTAfUGj+BuVF9AxIPmMN4TiKc3OyancFuOvSmbmE3qd68UM1cpd5/Wu1TyXYhjJvC9LB6Nxxkxc2IJ2r/Ptw5zbp5JHgqeO4+AWYhjkj9eERLuB0QbQPdQIDAQAB");
//        System.out.println(msg);
    }

    /**
     * RSA公钥加密
     */
    public static String encrypt(String str, String publicKey) {
        try {
            //base64编码的公钥
            byte[] decoded = Base64.Decoder(publicKey.getBytes(Charset.forName("utf-8")));
            if (decoded == null)
                return null;

            RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
            //RSA加密
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            return Base64.Encoder(cipher.doFinal(str.getBytes("UTF-8")));
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
